﻿using System;
using System.Collections;

namespace 合并区间
{
    class Program
    {
        public ArrayList merge(ArrayList intervals)
        {
            ArrayList nums = new ArrayList();
            ArrayList result = new ArrayList();
            double start;
            double end;
            int index = 0;
            foreach (int[] val in intervals)
            {
                for (double i = val[0]; i <= val[1]; i = i + 0.5)
                {
                    nums.Add(i);
                }
            };
            nums.Sort();
            start = (double)nums[0];
            foreach (double num in nums)
            {
                if ((double)nums[index + 1] - num > 0.5)
                {
                    end = (double)nums[index];
                    result.Add(new double[] { start, end });
                    start = (double)nums[index + 1];
                }
                if(index != nums.Count - 2) index++;
            }
            result.Add(new double[] { start, (double)nums[nums.Count - 1] });
            return result;
        }
        static void Main(string[] args)
        {
            Program program = new Program();
            //定义测试集intervals
            ArrayList intervals = new ArrayList();
            int[] nums1 = new int[] {1,4};
            int[] nums2 = new int[] {4,5};
            intervals.Add(nums1);
            intervals.Add(nums2);
            Console.WriteLine("合并区间为：");
            foreach (double[] al in program.merge(intervals)) {
                Console.Write("[");
                foreach (double num in al) {
                    Console.Write(' '); Console.Write(num); 
                }
                Console.Write("]");
            };
        }
    }
}
